package Android.Billarm;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Bill stores all the data related to a certain bill.
 * 
 * @author whiskahs
 *
 */
public class Bill implements Serializable{

	public long id;
    public boolean activep;
	private long amount; /* Dollars * 100 + cents */
	private DBISODate billarmDate;
	public List<Integer> icons; /* Icons are keyed in R.java */
    public boolean dirtyp = false;

	/**
	 * Empty constructor
	 */
    public Bill(){
		amount = 0;	
        id = -1;
        activep = true;
        icons = new ArrayList<Integer> ();
        billarmDate = new DBISODate ();
	}
	
	public Bill(long id, long amount, DBISODate billarmDate, List<Integer> icons, boolean activep) {
		this.id = id;
		this.amount = amount;
		this.billarmDate = billarmDate;
		this.icons = icons;
        this.activep = activep;
	}
	
	public Bill(long id, long amount, DBISODate billarmDate, String iconsString, boolean activep) {
		this.id = id;
		this.amount = amount;
		this.billarmDate = billarmDate;
		this.icons = stringToIntegerList(iconsString);
        this.activep = activep;
	}

	/**
	 * Copy constructor
	 */
	public Bill(Bill b){
        id = -1;
		amount = b.getAmount();	
        icons = new ArrayList<Integer> ();
        billarmDate = new DBISODate ();
	}
	
	public long getAmount(){
		return amount;
	}

    public String getAmountString ()
    {
        int cents = (int) (amount % 100);
        return amount / 100
            +  (cents < 10 ? ".0" : ".")
            +  cents;
    }
	
	public void setAmount(long new_amount){
        amount = new_amount;
	}

	public DBISODate getBillarmDate() {
        return billarmDate;
    }
		 
	public void setBillarmDate(DBISODate billarmDate) {
	   this.billarmDate = billarmDate;
	}
	
	public void setIcons(List<Integer> icons){
		this.icons = icons;
	}

	public List<Integer> getIcons() {
		return this.icons;
	}
	
	//returns icons as one string of comma separated values 
	public String getIconsCommaSeparated() {
		return integerListToCommaSeparated(this.icons);
	}
	
	//Converts a List<Integer> to a String of comma separated values
	private String integerListToCommaSeparated(List<Integer> intList) {
		String CommaSeparatedList = "";
		int i =0;
		
		for(; i < intList.size()-1; i++)
			CommaSeparatedList += String.valueOf(intList.get(i)) + ",";
		if(i < intList.size())
			CommaSeparatedList += String.valueOf(intList.get(i));
		return CommaSeparatedList;
	}
	//Converts a String to a List<Integer> of comma separated values
	private List<Integer> stringToIntegerList(String CommaSeparatedList) {
		List<Integer> intList = new ArrayList<Integer> ();
		String[] stringValues = CommaSeparatedList.split(",");
		
        /* If there's no icons, don't fail */
        if (stringValues.length == 1 && stringValues[0].length() == 0)
            return intList;

        for(int i =0; i < stringValues.length; i++) {
            intList.add(Integer.parseInt(stringValues[i]));
        }
		return intList;
	}
	
}

